import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  TextEditingController ipaddress = TextEditingController(text: "");
  TextEditingController port = TextEditingController(text: "");
  TextEditingController line = TextEditingController(text: "");

  Future<void> _initValue() async {
    final prefs = await SharedPreferences.getInstance();
    final address = prefs.getString("address");
    final p = prefs.getString("port");
    final l = prefs.getString("line");
    setState(() {
      ipaddress.text = address!;
      port.text = p!;
      line.text = l!;
    });
  }
  Future<void> _saveSettings(BuildContext context) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString("address", ipaddress.text);
    await prefs.setString("port", port.text);
    await prefs.setString("line", line.text);
    // 可以添加保存成功的提示
    TDToast.showSuccess("设置保存成功", context: context);
  }
  @override
  void initState() {
    super.initState();
    _initValue();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(padding: EdgeInsets.all(20),child:  ListView(
      children: [
        Row(
          children: [
            TDInput(
              leftLabel: 'IP地址:',
              controller: ipaddress,
              width: 300,
              backgroundColor: Colors.white,
              inputType: TextInputType.text,
              hintText: '请求的IP地址',
              onChanged: (text) {
                setState(() {
                  ipaddress.text = text;
                });
              },
              onClearTap: () {
                ipaddress.clear();
                setState(() {});
              },
            ),
            SizedBox(width: 20),
            TDInput(
              leftLabel: 'Port端口:',
              controller: port,
              width: 300,
              backgroundColor: Colors.white,
              inputType: TextInputType.text,
              hintText: '请求的Port端口',
              onChanged: (text) {
                setState(() {
                  port.text = text;
                });
              },
              onClearTap: () {
                port.clear();
              },
            ),
          ],
        ),
        SizedBox(height: 20),
        Row(
          children: [
            TDInput(
              leftLabel: '线体位置:',
              controller: line,
              width: 300,
              backgroundColor: Colors.white,
              inputType: TextInputType.text,
              hintText: '线体位置',
              onChanged: (text) {
                setState(() {
                  line.text = text;
                });
              },
              onClearTap: () {
                line.clear();
              },
            ),
          ],
        ),
        SizedBox(height: 20),
        Align(
          alignment: Alignment.centerRight,
          child: Container(
            child: Padding(
              padding: EdgeInsets.only(right: 40),
              child: TDButton(text: "保存 设置", theme: TDButtonTheme.primary, onTap: () => _saveSettings(context),),
            ),
          ),
        ),
      ],
    ),);
  }
}
